package pt.inescid.components.policyengine.heimdall.database;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import pt.inescid.components.policyengine.heimdall.base.Policy;
import pt.inescid.components.policyengine.heimdall.commons.DBManager;
import pt.inescid.components.policyengine.heimdall.commons.Log;

public class PolicyDAO {
	

	/* ===========================================================
	 ==============================================================
	 =============                    =============================
	 ============= getAllRecords =====================
	 =============                    =============================
	 ==============================================================
	 =========================================================== */
	public static Vector getAllRecords() {
		
		ResultSet rs;
		Vector allRecords = new Vector();
		
		String query = "SELECT * FROM policy ORDER BY user;";
		
		try {
			rs = DBManager.executeQuery(query);
			
			if (rs.first()) {
				do {
					allRecords.add(createRecord(rs));
				} while (rs.next());
			}
			
		}
		catch (SQLException e) {
			Log.debug(Log.ERROR, "Erro SQL: " + e + 
					" - query = " + query, "EventObjectDAO.getAllRecordsAfter");
			allRecords = null;
		}
		
		return (allRecords);
	}; // getAllRecords
	
	

	/* ===========================================================
	 ==============================================================
	 =============                    =============================
	 ============= getRecord =====================
	 =============                    =============================
	 ==============================================================
	 =========================================================== */
	public static Policy getRecord(long currentUserId, String tag) {
		
		ResultSet rs;
		Policy currentPolicy;
		
		String query = "SELECT * FROM policy WHERE user=" + currentUserId + 
					" AND tag='" + tag + "';";
		
		try {
			rs = DBManager.executeQuery(query);
			
			if (rs.first()) {
				currentPolicy = createRecord(rs);
			}
			else {
				currentPolicy = null;
			};
			
		}
		catch (SQLException e) {
			Log.debug(Log.ERROR, "Erro SQL: " + e + 
					" - query = " + query, "EventObjectDAO.getRecord");
			currentPolicy = null;
		}
		
		return (currentPolicy);
	}; // getRecord
	
	
	/* ===========================================================
	 ==============================================================
	 =============                    =============================
	 =============      createRecord   =============================
	 =============                    =============================
	 ==============================================================
	 =========================================================== */
	
	public static Policy createRecord(ResultSet rs)
	throws SQLException {
		Policy newRecord = null;
		
		try {
			newRecord = new Policy(
					rs.getLong("id"),
					rs.getLong("user"),
					rs.getString("tag"),
					rs.getInt("type"),
					rs.getString("cesName"),
					rs.getString("info"));
		}
		catch (Exception e) {
			Log.debug(Log.ERROR, "Erro SQL: " + e, "PolicyDAO.createRecord");
			newRecord = null;
		}
		
		return (newRecord);
	}; // createRecord
	

	/* ===========================================================
	 ==============================================================
	 =============                    =============================
	 ============= insertRecord =====================
	 =============                    =============================
	 ==============================================================
	 =========================================================== */
	public static void insertRecord(long currentUserId, 
			int type, 
			String cesName,
			String tag,
			String policyText) {
		String query = "INSERT INTO policy(user,type,cesName,tag,info) " +
				"VALUES (?,?,?,?,?);";
		
		PreparedStatement ps;
		try {
			ps = DBManager.getConnection().prepareStatement(query);
			ps.setLong(1, currentUserId);
			ps.setInt(2, type);
			ps.setString(3, cesName);
			ps.setString(4, tag);
			ps.setString(5, policyText);
			ps.execute();
			ps.close();
		} catch (SQLException e) {
			Log.debug(Log.INFO, "SQL Problem: " + e + "query=" + query + ")", 
			"PolicyUploadFC.loadPolicy"); 	
		}
	}
	

	/* ===========================================================
	 ==============================================================
	 =============                    =============================
	 ============= updateRecord =====================
	 =============                    =============================
	 ==============================================================
	 =========================================================== */
	public static void updateRecord(long currentUserId, 
			String tag,
			String policyText) {
		String query = "UPDATE policy SET info=? WHERE user=? and tag=?;";
		
		PreparedStatement ps;
		try {
			ps = DBManager.getConnection().prepareStatement(query);
			ps.setString(1, policyText);
			ps.setLong(2, currentUserId);
			ps.setString(3, tag);
			ps.execute();
			ps.close();
		} catch (SQLException e) {
			Log.debug(Log.INFO, "SQL Problem", 
			"PolicyDAO.updateRecord"); 	
		}
	}
	

	public static void main(String s[]) {
//		Vector a = new Vector();
//		a.add("abc");
//		a.add(new Integer(123));
//		
//		EventObject record = new EventObject(1,a);
//		
//		insertRecord(record);
//		SplEvent ce = getRecord(8);
//		ce = null;
	}
	
}; // PolicyDAO
